package top.dbhg.tasklist.hud;

import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

public class HUD {

    private static TaskState taskState;
    private static final int animationSpeed = 5; // 每帧移动的像素数
    private static final int taskHeight = 22; // 每个任务框的高度
    private static final int taskWidth = 162; // 每个任务框的宽度
    private static final int taskPadding = 5; // 任务框之间的间距

    public static void addTask(String tag, String text, Formatting color) {
        taskState.addTask(new TaskState.Task(tag, text, color));
    }

    public static void completeTask(String tag) {
        if (taskState != null) {
            taskState.getTasks().stream().filter(task -> task.tag.equals(tag)).findFirst().ifPresent(taskToRemove -> taskToRemove.visible = false);
        }
    }

    public static void reSet(){
        CopyOnWriteArrayList<TaskState.Task> tasks = taskState.getTasks();
        for (TaskState.Task task : tasks) {
            completeTask(task.tag);
        }
    }

    public static List<String> getTaskTags() {
        if (taskState != null) {
            return taskState.getTasks().stream()
                    .map(task -> task.tag)
                    .collect(Collectors.toList());
        }
        return List.of();
    }

    public static void register() {
        HudRenderCallback.EVENT.register((context, tickDelta) -> {
            MinecraftClient client = MinecraftClient.getInstance();
            taskState = TaskState.getServerState(Objects.requireNonNull(client.getServer()));
            TextRenderer textRenderer = client.textRenderer;
            if (client.player != null && taskState != null) {
                int yOffset = 5;
                CopyOnWriteArrayList<TaskState.Task> tasks = taskState.getTasks();
                for (TaskState.Task task : tasks) {
                    if (task.visible && task.animationX < 2) {
                        task.animationX += animationSpeed;
                        if (task.animationX > 2) {
                            task.animationX = 2;
                        }
                    } else if (!task.visible && task.animationX > -taskWidth) {
                        task.animationX -= animationSpeed;
                        if (task.animationX <= -taskWidth) {
                            taskState.removeTask(task.tag);
                        }
                    }

                    int areaX = task.animationX;
                    int areaY = yOffset;
                    int textWidth = textRenderer.getWidth(task.text);
                    int textX = areaX + (taskWidth - textWidth) / 2;
                    int textY = areaY + (taskHeight - textRenderer.fontHeight) / 2;

                    Identifier texture = new Identifier("tasklist", "textures/ui/taskhub.png");
                    context.drawTexture(texture, areaX, areaY, 0, 0, taskWidth, taskHeight, taskWidth, taskHeight);

                    // Draw the text centered in the area
                    context.drawTextWithShadow(textRenderer, task.text, textX, textY, task.color.getColorValue());

                    yOffset += taskHeight + taskPadding;
                }
            }
        });

    }
}
